package com.xianbeiying.app.ckgl.model;

import android.content.Context;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.WeatherBean;
import com.xianbeiying.app.ckgl.http.HttpPostManger;
import com.xianbeiying.app.ckgl.util.DataUtil;
import com.xianbeiying.app.ckgl.util.DialogUtile;
import com.xianbeiying.app.ckgl.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import cz.msebera.android.httpclient.Header;

/**
 * 天气获取的接口
 */
public class WeatherModel extends BaseModel{
    private Context context;
    private RequestParams params;
    private AsyncHttpClient client;
    public WeatherModel(Context context){
        client = new AsyncHttpClient();
        params = new RequestParams();
        this.context = context;
        this.params = params;
    }
    //获取当地城市的天气
    public void getWeatherData(final String city,boolean isShow){
        if (isShow) {
            DialogUtile.showLoading(context);
        }
        params.put("format", 1);
        params.put("cityname", city);
        params.put("key", "11aecfdc2e7e9946d3b07cb333eb0232");
        HttpPostManger.get("http://v.juhe.cn/weather/index?", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                //关闭加载
                DialogUtile.closeDialog();
                try {
                    JSONObject object = new JSONObject(new String(responseBody));
                    object.getString("resultcode");
                    if (object.getString("resultcode").equals("200")) {
                        String result = object.getString("result");
                        if (result.equals("")) {
                            return;
                        }
                        JSONObject result_object = new JSONObject(result);
                        String future = result_object.getString("future");
                        if (future.equals("")) {
                            return;
                        }
                        JSONObject future_object = new JSONObject(future);
                        if (future_object.equals("")) {
                            return;
                        }
                        Format f = new SimpleDateFormat("yyyyMMdd");
                        Calendar c = Calendar.getInstance();
                        LogUtils.e("cccccc==" + f.format(c.getTime()));
                        LogUtils.e(future_object.getString("day_" + DataUtil.getDataFouter(0))+"====");
                        WeatherBean weatherBean= JSON.parseObject(future_object.getString("day_" + DataUtil.getDataFouter(0)), WeatherBean.class);
                        WeatherModel.this.onCallBackData("天气",weatherBean);
//                        tv_temadd.setText(weatherBean.getTemperature()+"\n"+listBean.getName());   //28\n西安
//                        setImage(weatherBean.getWeather(),img_weather);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                com.xianbeiying.app.ckgl.util.LogUtils.e("返回值", statusCode + "----------------");
                if(statusCode==0){
                    ToastUtil.showInfo(context,"无法连接平台");
                }else{
                    ToastUtil.showInfo(context,"无法连接平台");
                }
            }
        });

    }
    public static boolean getString(String str, String s) {
        return str.matches(".*" + s + ".*");
    }
    public void setImage(String mark, ImageView imageView) {
//        boolean b1 = getString(mark, "多云");
//        if (b1) {
//            mark = "多云";
//        }
        switch (mark) {
            case "晴": {
                imageView.setImageResource(R.mipmap.qing);
                break;
            }
            case "多云": {
                imageView.setImageResource(R.mipmap.duoyun);
                break;
            }
            case "阴": {
                imageView.setImageResource(R.mipmap.yin);
                break;
            }
            case "阵雨": {
                imageView.setImageResource(R.mipmap.zhenyu);
                break;
            }
            case "雷阵雨": {
                imageView.setImageResource(R.mipmap.leizhenyu);
                break;
            }
            case "雷阵雨伴有冰雹": {
                imageView.setImageResource(R.mipmap.leizhenyubanyoubingbao);
                break;
            }
            case "雨夹雪": {
                imageView.setImageResource(R.mipmap.yujiaxue);
                break;
            }
            case "小雨": {
                imageView.setImageResource(R.mipmap.xiaoyu);
                break;
            }
            case "中雨": {
                imageView.setImageResource(R.mipmap.zhongyu);
                break;
            }
            case "大雨": {
                imageView.setImageResource(R.mipmap.dayu);
                break;
            }
            case "暴雨": {
                imageView.setImageResource(R.mipmap.baoxue);
                break;
            }
            case "大暴雨": {
                imageView.setImageResource(R.mipmap.dabaoyu);
                break;
            }
            case "特大暴雨": {
                imageView.setImageResource(R.mipmap.tedabaoyu);
                break;
            }
            case "阵雪": {
                imageView.setImageResource(R.mipmap.zhenxue);
                break;
            }
            case "小雪": {
                imageView.setImageResource(R.mipmap.xiaoxue);
                break;
            }
            case "中雪": {
                imageView.setImageResource(R.mipmap.zhongxue);
                break;
            }
            case "大雪": {
                imageView.setImageResource(R.mipmap.daxue);
                break;
            }
            case "暴雪": {
                imageView.setImageResource(R.mipmap.baoxue);
                break;
            }
            case "雾": {
                imageView.setImageResource(R.mipmap.wu);
                break;
            }
            case "冻雨": {
                imageView.setImageResource(R.mipmap.dongyu);
                break;
            }
            case "沙尘暴": {
                imageView.setImageResource(R.mipmap.shachenbao);
                break;
            }
            case "小雨-中雨": {
                imageView.setImageResource(R.mipmap.xiaoyu_zhongyu);
                break;
            }
            case "中雨-大雨": {
                imageView.setImageResource(R.mipmap.zhongyu_dayu);
                break;
            }
            case "大雨-暴雨": {
                imageView.setImageResource(R.mipmap.dayu_baoyu);
                break;
            }
            case "暴雨-大暴雨": {
                imageView.setImageResource(R.mipmap.baoyu_dabaoyu);
                break;
            }
            case "大暴雨-特大暴雨": {
                imageView.setImageResource(R.mipmap.dabaoyu_tedabaoyu);
                break;
            }
            case "小雪-中雪": {
                imageView.setImageResource(R.mipmap.xiaoxue_zhongxue);
                break;
            }
            case "中雪-大雪": {
                imageView.setImageResource(R.mipmap.zhongxue_daxue);
                break;
            }
            case "大雪-暴雪": {
                imageView.setImageResource(R.mipmap.daxue_baoxue);
                break;
            }
            case "浮尘": {
                imageView.setImageResource(R.mipmap.fuchen);
                break;
            }
            case "扬沙": {
                imageView.setImageResource(R.mipmap.yangsha);
                break;
            }
            case "强沙尘暴": {
                imageView.setImageResource(R.mipmap.qiangshachenbao);
                break;
            }
            case "霾": {
                imageView.setImageResource(R.mipmap.wumai);
                break;
            }
            default:
                imageView.setImageResource(R.mipmap.qing);
                break;
        }
    }

    /**
     * 获取某个地区的天气情况  http://wthrcdn.etouch.cn/weather_mini?city
     * */
    public void getWeather(final String city,boolean isShow){
        if (isShow) {
            DialogUtile.showLoading(context);
        }
        String name=null;
        if(city.contains("市")){
            name=city.substring(0,city.length()-1);
        }else{
            name=city;
        }
        LogUtils.e(name+"==============");
        HttpPostManger.get("http://wthrcdn.etouch.cn/weather_mini?city="+name, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody){
                //关闭加载
                DialogUtile.closeDialog();
                LogUtils.e("-----------------"+new String(responseBody));
                WeatherModel.this.onCallBackData("大棚天气",responseBody);
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                //关闭加载
                DialogUtile.closeDialog();
                ToastUtil.showInfo(context,"获取数据失败");
                com.xianbeiying.app.ckgl.util.LogUtils.e("返回值", statusCode + "----------------");
            }
        });

    }
}
